package model.xmlconvert;
import model.xmlconvert.ModelLinkFlowChartComponent.ArrowType;
import nu.xom.Element;

public class ModelDiamondFlowChartComponent extends ModelFlowChartComponent{

	private ModelDiamondValidator validator = new ModelDiamondValidator();
	
	public ModelDiamondFlowChartComponent(String s){
		super(s);
	}

	@Override
	public Element getXML() {
	
		Element ifelement = new Element("if");
		Element condition = new Element("condition");
		condition.appendChild(this.getOperation());
		ifelement.appendChild(condition);
		
		for(ModelLinkFlowChartComponent m : super.connectedComponentsList){
			
			if(m.getArrowType() == ArrowType.YES){
				ifelement.appendChild(m.getXML());
			}else if(m.getArrowType() == ArrowType.NO){
				ifelement.appendChild(m.getXML());
			}
		}
		
		return ifelement;
	}

	@Override
	public boolean addComponentConnection(ModelFlowChartComponent m) {
		
		ArrowType arrow;
		
		if(super.connectedComponentsList.size() <= 2){
			
			if(super.connectedComponentsList.size() == 1){
				if(this.connectedComponentsList.get(0).getArrowType() == ArrowType.YES)
					arrow = ArrowType.NO;
				else
					arrow = ArrowType.YES;
				
				this.connectedComponentsList.add(ModelChartFactory.createDiamondArrrowComponent(this, m, arrow));
				return true;
			}else{
				this.connectedComponentsList.add(ModelChartFactory.createDiamondArrrowComponent(this, m, ArrowType.YES));
				return true;
			}
		}else
			return false;
	}
	
	public boolean addComponentConnection(ModelFlowChartComponent m, ArrowType type) {
		
		if(super.connectedComponentsList.size() <= 2){
			
			if(super.connectedComponentsList.size() == 1){
				if(this.connectedComponentsList.get(0).getArrowType() == type)
					return false;
			}
			
			this.connectedComponentsList.add(ModelChartFactory.createDiamondArrrowComponent(this, m, type));
			return true;
		}else
			return false;
	}
	

	@Override
	public boolean setOperation(String operation) {
		
		if(validator.validateExpression(operation)){
			super.operation = operation;
			return true;
		}else
			return false;
	}
}